<template>
  <div>
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <van-cell title="头像" is-link @click="$refs.fileRef.click()">
      <van-image
        round
        fit="cover"
        class="avator"
        :src="user.photo"
      />
    </van-cell>
    <input type="file" ref="fileRef" hidden @change="onFileChange">
    <van-cell title="昵称" :value="user.name" is-link @click="isUpdateNameShow = true"></van-cell>
    <!-- <van-cell title="性别" :value="user.gender === 1? '女':'男'" is-link></van-cell> -->
    <van-cell title="性别" :value="['男','女'][user.gender]" is-link @click="isUpdateGenderShow = true"></van-cell>
    <van-cell title="生日" :value="user.birthday" is-link @click="isUpdateBirthdayShow = true"></van-cell>
    <van-popup v-model="isUpdateNameShow" position="bottom" :style="{ height: '100%' }">
      <update-name v-if="isUpdateNameShow" @close="isUpdateNameShow = false" v-model="user.name"></update-name>
    </van-popup>
    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <update-gender v-if="isUpdateGenderShow" @close="isUpdateGenderShow = false" v-model="user.gender"></update-gender>
    </van-popup>
    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
      <update-birthday v-if="isUpdateBirthdayShow" @close="isUpdateBirthdayShow = false" v-model="user.birthday"></update-birthday>
    </van-popup>
    <van-popup v-model="isUpdatePhotoShow" position="bottom"  :style="{ height: '100%' }">
      <update-photo v-if="isUpdatePhotoShow" @close="isUpdatePhotoShow = false" :img="imgPath" @update-photo="user.photo = $event"></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserProfile',
  props: {},
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data () {
    return {
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      user: {

      },
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      imgPath: ''

    }
  },
  async created () {
    const { data: { data } } = await getUserProfile()
    this.user = data
  },
  methods: {
    onFileChange () {
      console.log('onFileChange', this.$refs.fileRef.files)
      const imgPath = URL.createObjectURL(this.$refs.fileRef.files[0])
      this.imgPath = imgPath
      this.isUpdatePhotoShow = true
      this.$refs.fileRef.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.avator {
  width: 60px;
  height: 60px;
}
  .van-popup {
    background-color: #f5f7f9;
  }
</style>
